package com.huawei.esdk.uc.device;

import com.huawei.esdk.platform.common.MessageContext;
import com.huawei.esdk.platform.common.ThreadLocalHolder;
import com.huawei.esdk.platform.common.bean.aa.AccountInfo;
import com.huawei.esdk.platform.common.constants.ESDKConstant;
import com.huawei.esdk.platform.common.utils.ApplicationContextUtil;
import com.huawei.esdk.platform.nemgr.base.MultiConnDeviceBase;
import com.huawei.esdk.platform.nemgr.itf.IDeviceConnection;
import com.huawei.esdk.platform.nemgr.itf.IDeviceManager;
import com.huawei.esdk.uc.device.obg.INotifyCapability;
import com.huawei.esdk.uc.device.u19.IAttendeeCapability;
import com.huawei.esdk.uc.device.u19.ICTCU19Capability;
import com.huawei.esdk.uc.device.u19.attendee.AttendeeCapabilityImpl;
import com.huawei.esdk.uc.device.u19.callback.U19NotifyCapabilityImpl;
import com.huawei.esdk.uc.device.u19.ctc.CTCU19CapabilityImpl;

public class U19V1R1C20Device extends MultiConnDeviceBase
{
    private IDeviceManager deviceManager = ApplicationContextUtil.getBean("deviceManager");
    
    private String sap;
    
    protected String getSap()
    {
        return sap;
    }
    
    protected void setSap(String sap)
    {
        this.sap = sap;
    }
    
    public U19V1R1C20Device(String sap)
    {
        this.sap = sap;
        prepareDeviceCapability();
    }
    
    protected void prepareDeviceCapability()
    {
        addServiceObjectMap(ICTCU19Capability.class, new CTCU19CapabilityImpl());
        addServiceObjectMap(IAttendeeCapability.class, new AttendeeCapabilityImpl());
        addServiceObjectMap(INotifyCapability.class, new U19NotifyCapabilityImpl());
    }
    
    @Override
    public IDeviceConnection createConnection(String appId, String sap, String user, String pwd)
    {
        return new U19Connection(this);
    }
    
    @Override
    public void prepareAuthInfo(String user, String pwd)
    {
        
    }
    
    @Override
    public String getConnIdFromContext()
    {
        String id = "";
        MessageContext mc = ThreadLocalHolder.get();
        if (null != mc)
        {
            AccountInfo acctInfo = (AccountInfo)mc.getEntities().get(ESDKConstant.ACCT_INFO_ESDK);
            if (null != acctInfo)
            {
                AccountInfo devAcctInfo =
                    authorizePolicy.getDeviceAccountInfo(acctInfo.getUserId(), acctInfo.getPassword());
                id = devAcctInfo.getUserId();// + acctInfo.getPassword();
            }
        }
        return id;
    }
    
    @Override
    public Boolean releaseConns()
    {
        for (String key : id2Connection.keySet())
        {
            IDeviceConnection conn = id2Connection.get(key);
            deviceManager.releaseConn(conn);
        }
        return true;
    }
    
}
